package com.stephen.jsbridge.utils;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;

import com.stephen.jsbridge.jsbridge.OnWebViewListener;

import org.xwalk.core.XWalkView;

/**
 * Created by Administrator on 2016/5/7.
 */
public abstract class OnMyWebViewListener implements OnWebViewListener {
    private Context context;
    private boolean isResponseClick;
    private StephenCommonNoDataView stephenCommonNoDataView;

    public OnMyWebViewListener(Context context, boolean isShowLoading, boolean isResponseClick, StephenCommonNoDataView stephenCommonNoDataView) {
        this.context = context;
        this.isResponseClick = isResponseClick;
        this.stephenCommonNoDataView = stephenCommonNoDataView;
    }

    @Override
    public void onReceivedTitle(XWalkView view, String title) {
        System.out.println("=============>获取该页面的Title:"+title);
        if(!TextUtils.isEmpty(title)){
            if(null != stephenCommonNoDataView && (title.equals("找不到网页") || title.toLowerCase().equals("错误")
                    || title.toLowerCase().equals("404 not found") || title.toLowerCase().contains("error") || title.toLowerCase().contains("网页无法打开"))){
                stephenCommonNoDataView.setNoDataViewShow(isResponseClick,"页面渲染失败"+(isResponseClick ? ",点击返回..." : ",重启App再进入..."));
            }//end of if
        }else{//title获取为空的情况,是有时加载服务器页面,服务器返回的html代码只有"<head></head><body></body>",造成app这边显示白板,所以根据这种情况可尝试重试
            onReceivedTitleEmpty(isResponseClick);
        }
    }

    @Override
    public void onPageStarted(XWalkView view, String url) {
        //if(null != stephenCommonNoDataView)stephenCommonNoDataView.setNoDataViewShow(false,"请稍候,页面加载中...");
        StephenToolUtils.showShortHintInfo(context,"页面开始加载...");
    }
    @Override
    public void onPageFinished(XWalkView view, String url) {
        StephenToolUtils.showShortHintInfo(context,"页面加载完成...");
        //if(!isLoadError && null != stephenCommonNoDataView)stephenCommonNoDataView.setNoDataViewHide();
        String title = (null != view) ? view.getTitle() : "占位Title,避免误判";
        System.out.println("===========onPageFinished====title===>"+title);
        if(TextUtils.isEmpty(title))onReceivedTitleEmpty(isResponseClick);
    }

    @Override
    public boolean onOverrideUrlLoading(XWalkView view, String url) {
        //System.out.println("=========onOverrideUrlLoading====>"+url);
        return true;
    }

    @Override
    public void onReceivedError(XWalkView view, int errorCode, String description, String failingUrl) {
        //System.out.println(errorCode+"======load "+failingUrl+" error==========>"+description);
        if(null != stephenCommonNoDataView)stephenCommonNoDataView.setNoDataViewShow(true,"页面加载错误:\n"+description);
    }

    public void onReceivedTitleEmpty(boolean isResponseClick){}
}
